package com.cykj.pos.controller.task;

import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.domain.BizMessageRecords;
import com.cykj.pos.domain.BizTimeoutIntegral;
import com.cykj.pos.service.*;
import com.cykj.pos.websocket.server.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

@Component
@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    IBizWalletService iBizWalletService;

    @Autowired
    IBizMerchOrderService iBizMerchOrderService;

    @Autowired
    IBizMerchantService bizMerchantService;

    @Autowired
    private WebSocketServer webSocketServer;
    @Autowired
    private  IBizTransactionAmount bizTransactionAmount;

    @Autowired
    private IBizMerchantInfoService iBizMerchantInfoService;

    @Value("${cykj.path}")
    private String path;
    // 计时任务 10s一次
    //
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void timingTask(){
        //System.out.println("11111111111");
        // 查询所有 字段为未超时的字段 临时积分表
        List<BizTimeoutIntegral> list = iBizWalletService.queryNotTimeoutOrder();
        // 判断是否超时
        Date date = new Date();// 当前时间
        for (BizTimeoutIntegral bizTimeoutIntegral : list){
            Long endTime = date.getTime();// 现在的时间毫秒数
            Long startTime = bizTimeoutIntegral.getCreateTime().getTime();// 兑换积分的开始时间
            // 如果时间超过12小时
            Long twelveHours = 43200000L;// 十二小时
            Long overTime = startTime+twelveHours;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = new Date(overTime);
            if (endTime-startTime>twelveHours){// 超时
                iBizWalletService.updateTemporaryIntegral(bizTimeoutIntegral);// 更改临时积分表
                iBizMerchOrderService.updateOrderByOrderNo(bizTimeoutIntegral);// 改变订单为超时
                // 兑换积分的积分返还
                iBizMerchOrderService.integralReturn(bizTimeoutIntegral);// 积分返还

                BizMerchant bizMerchant = bizMerchantService.getMerchantByMerchId(bizTimeoutIntegral.getMerchId());//
                BizMerchant parentMerchant = bizMerchantService.getMerchantByMerchId(bizTimeoutIntegral.getParentId());//
                // 发送消息吧
                // -----------------------  给发放机具人 发送消息 ----------------------------------
                BizMessageRecords chaoShiMessageRecords = new BizMessageRecords();
                // 发消息   6- 兑换申请
                String duiHuanMess = "您来自" + bizMerchant.getMerchName() + "的机具兑换申请于"+ sf.format(date1) +"超时";
                chaoShiMessageRecords.setMsgContent(duiHuanMess);// 消息内容
                chaoShiMessageRecords.setMsgType(9); // 消息类型  积分兑换 超时
                chaoShiMessageRecords.setReadStatus(0);// 消息未读
                chaoShiMessageRecords.setAdviceType(1); // 业务消息
                webSocketServer.sendInfo(parentMerchant.getUserId(), chaoShiMessageRecords);// 发送消息
                // -----------------------  给积分兑换人 发送消息 ----------------------------------
                BizMessageRecords duiHuanMessageRecords = new BizMessageRecords();
                // 发消息   6- 兑换申请
                String merchantMess = "您向" + parentMerchant.getMerchName() + "发起的积分兑换申请"+ sf.format(date1) +"超时";
                duiHuanMessageRecords.setMsgContent(merchantMess);// 消息内容
                duiHuanMessageRecords.setMsgType(9); // 消息类型  积分兑换 超时
                duiHuanMessageRecords.setReadStatus(0);// 消息未读
                duiHuanMessageRecords.setAdviceType(1); // 业务消息
                webSocketServer.sendInfo(bizMerchant.getUserId(), duiHuanMessageRecords);// 发送消息
            }else {
                continue;
            }
        }
    }


    @Scheduled(cron = "0 0 0 * * ?")   // 每日凌晨0点执行   交易表
    @PostMapping("/executeDayDataToThisMonthData")
    public void executeDayDataToThisMonthData() throws IOException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************每日清空日交易表数据不在此服务器执行*****************");
            return;
        }

        System.out.println("----------------------------------------------");
        System.out.println("每日清空日交易表数据开始");
        bizTransactionAmount.executeDayDataToThisMonthData();
        System.out.println("每日清空日交易表数据结束");
        System.out.println("----------------------------------------------");
    }


    @Scheduled(cron = "0 0 0 * * ?")   // 每日凌晨0点执行   累计表
    @PostMapping("/executeDayTotalToThisMonthData")
    public void executeDayTotalToThisMonthData() throws IOException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************处理今日字段数据转移至昨日数据不在此服务器执行*****************");
            return;
        }
        System.out.println("----------------------------------------------");
        System.out.println("每日处理今日字段数据转移至昨日数据,清空当日数据-开始");
        bizTransactionAmount.executeDayTotalToThisMonthData();
        System.out.println("每日处理今日字段数据转移至昨日数据,清空当日数据-结束");
        System.out.println("----------------------------------------------");
    }


    // 未激活 还得扣钱
//    @Scheduled(cron = "0 0 0 * * ?")   // 每日凌晨0点执行   累计表
    @PostMapping("/unActive")
    public void unActive() throws ParseException, IOException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************每日查询扣款的设备不在此服务器执行*****************");
            return;
        }
        System.out.println("每日查询扣款的设备-开始");
        bizTransactionAmount.unActive();
        System.out.println("每日查询扣款的设备-结束");
    }


    /*
    * 每月1号零点定时器  @Scheduled(cron0 0 0 1 * ?)
    * */
    @Scheduled(cron = "0 0 0 1 * ?")   // 每月1日凌晨0点执行   本月转移至上月  本月清空,
    @PostMapping("/executeThisMonthDataToLastMonthDate")
    public void executeThisMonthDataToLastMonthDate() throws IOException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************每月1日处理商户/代理的本月/上月交易额不在此服务器执行*****************");
            return;
        }
        System.out.println("----------------------------------------------");
        System.out.println("每月1日处理商户/代理的本月/上月交易额开始");
        bizTransactionAmount.executeThisMonthDataToLastMonthDate();
        System.out.println("每月1日处理商户/代理的本月/上月交易额结束");
        System.out.println("----------------------------------------------");
    }


    public Properties getMyProperty() throws IOException {
        InputStream in = new BufferedInputStream(new FileInputStream(path+"/cms.properties"));
        Properties p = new Properties();
        p.load(in);
        return p;
    }


    // 沉默商户
    @Scheduled(cron = "0 1 0 * * ?")// 每日零点1分执行   沉默商户
    @PostMapping("/silence/merchantInfo")
    public void silenceMerchantInfo() throws IOException, ParseException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************沉默商户统计*****************");
            return;
        }
        System.out.println("查询沉默商户-开始");
        iBizMerchantInfoService.silenceMerchantInfo();
        System.out.println("查询沉默商户-结束");
    }

    //未激活
    @Scheduled(cron = "0 1 0 * * ?")// 每日零点1分执行   未激活
    @PostMapping("/unActive/merchantInfo")
    public void unActiveMerchantInfo() throws IOException, ParseException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************未激活商户统计*****************");
            return;
        }
        System.out.println("查询未激活商户-开始");
        iBizMerchantInfoService.selectUnActiveMerchantInfo();
        System.out.println("查询未激活商户-结束");
    }


    //伪激活
    @Scheduled(cron = "0 1 0 * * ?")// 每日零点1分执行   未激活
    @PostMapping("/failure/merchantInfo")
    public void failureActiveMerchantInfo() throws IOException, ParseException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************伪激活商户统计*****************");
            return;
        }
        System.out.println("查询伪激活商户-开始");
        iBizMerchantInfoService.selectFailureActiveMerchantInfo();
        System.out.println("查询伪激活商户-结束");
    }


//    @Scheduled(cron = "0 30 0 * * ?")// 每日零点30分执行    创建流量费
    public void executeCreateFlowTask() throws IOException, ParseException {
        Properties properties = this.getMyProperty();
        if(!"2".equals(properties.getProperty("type"))){
            System.out.println("**************创建流量费*****************");
            return;
        }
        System.out.println("创建流量费-开始");
        iBizMerchantInfoService.executeCreateFlowTask();
        System.out.println("创建流量费-结束");
    }
}
